package com.ouman.luoliluoli;

import android.animation.Animator;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.ouman.luoliluoli.mainfragments.HotFragment;
import com.ouman.luoliluoli.mainfragments.LuoliTalkFragment;
import com.ouman.luoliluoli.moviefragments.MovieFragmentPageAdapter;
import com.ouman.luoliluoli.moviefragments.NewestMovieFragment;
import com.ouman.luoliluoli.moviefragments.TelevisionFragment;

import static android.R.attr.content;

public class MainFragment extends Fragment {


    private OnFragmentInteractionListener mListener;
    private int themeColor;

    private int[] tabIcons = {
            R.drawable.ic_fire_black,
            R.drawable.ic_flower_black_24px,
    };

    public MainFragment() {
        // Required empty public constructor
    }


    public static MainFragment newInstance(String param1, String param2) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TypedValue typedValue = new TypedValue();
        getActivity().getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        themeColor = typedValue.data;
        System.out.println(themeColor);
        System.out.println("[IMPORTANT] got activity primary color: " + themeColor);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_main, container, false);
        ((MainActivity) getActivity()).setAppBarElevation(0);
        ((MainActivity) getActivity()).disableCollapsingLayoutTitle();
        ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("首页");


        final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPagerMain);
        setupViewPager(viewPager);

        //find tab layout in article fragment
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayoutMain);
        tabLayout.setBackgroundColor(themeColor);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setElevation(16);
        setupTabIcons(tabLayout);
        return view;
    }

    private void setupTabIcons(TabLayout tabLayout) {
        try{
            tabLayout.getTabAt(0).setIcon(tabIcons[0]);
            tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        } catch (NullPointerException e){
            System.out.println(e);
        }
    }

    private void setupViewPager(ViewPager viewPager) {
        MovieFragmentPageAdapter adapter = new MovieFragmentPageAdapter(getChildFragmentManager());
        adapter.addFragment(new HotFragment(), "");
        adapter.addFragment(new LuoliTalkFragment(), "");
        viewPager.setAdapter(adapter);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
